#! /bin/bash

export VERSION="50"
export SYSTEM_DESC="ChimeraOS"
export SYSTEM_NAME="chimeraos"
export USERNAME="gamer"
export SIZE="12000MB"
export ARCHIVE_DATE=$(date -d 'yesterday' +%Y/%m/%d)
export WEBSITE="https://chimeraos.org"
export DOCUMENTATION_URL="https://chimeraos.org/about"
export BUG_REPORT_URL="https://github.com/ChimeraOS/chimeraos/issues"

export KERNEL_PACKAGE="linux"
export KERNEL_PACKAGE_ORIGIN="repo"

export PACKAGES="\
	accountsservice \
	acpi_call-dkms \
	acsccid \
	alsa-firmware \
	alsa-utils \
	amd-debug-tools \
	amd-ucode \
	bash-completion \
	broadcom-wl-dkms \
	bzip2 \
	cifs-utils \
	cpupower \
	diffutils \
	dkms \
	distrobox \
	dmidecode \
	dosbox \
	efibootmgr \
	epiphany \
	ethtool \
	evtest \
	fakeroot \
	ffmpeg \
	file \
	ffmpegthumbnailer \
	firejail \
	flatpak \
	fmt \
	fuse-zip \
	fuse2 \
	fwupd \
	gamescope \
	git \
	gnome-browser-connector \
	gnome-console \
	gnome-control-center \
	gnome-disk-utility \
	gnome-keyring \
	gnome-menus \
	gnome-session \
	gnome-shell \
	gnome-software \
	gnome-system-monitor \
	gnome-text-editor \
	gnome-tweaks \
	gst-plugin-pipewire \
	gvfs-smb \
	gvfs-nfs \
	gzip \
	htop \
	intel-gpu-tools \
	intel-media-driver \
	intel-ucode \
	intel-undervolt \
	less \
	lib32-curl \
	lib32-fontconfig \
	lib32-freetype2 \
	lib32-libgpg-error \
	lib32-libnm \
	lib32-libxinerama \
	lib32-libxcrypt-compat \
	lib32-mangohud \
	lib32-openal \
	lib32-pipewire \
	lib32-sdl2 \
	lib32-systemd \
	lib32-vulkan-icd-loader \
	libnfc \
	libretro-beetle-pce-fast \
	libretro-beetle-psx-hw \
	libretro-desmume \
	libretro-dolphin \
	libretro-flycast \
	libretro-genesis-plus-gx \
	libretro-kronos \
	libretro-mame \
	libretro-mesen-s \
	libretro-mgba \
	libretro-mupen64plus-next \
	libretro-nestopia \
	libretro-picodrive \
	libretro-ppsspp \
	libretro-shaders-slang \
	libretro-snes9x \
	libxcrypt-compat \
	libxss \
	lightdm \
	linux-firmware \
	liquidctl \
	logrotate \
	lrzip \
	loupe \
	lshw \
	mangohud \
	mesa-demos \
	modemmanager \
	nano \
	nautilus \
	networkmanager \
	nfs-utils \
	noto-fonts-emoji \
	nss-mdns \
	nvidia-open-dkms \
	opencl-nvidia \
	lib32-opencl-nvidia \
	nvidia-utils \
	lib32-nvidia-utils \
	nvidia-prime \
	openal \
	openrazer-daemon \
	openssh \
	p7zip \
	pipewire \
	pipewire-alsa \
	pipewire-jack \
	pipewire-pulse \
	podman \
	pulsemixer \
	python \
	python-notify2 \
	python-pyscard \
	qtractor \
	retroarch \
	rsync \
	smbclient \
	sof-firmware \
	sshfs \
	steam \
	sudo \
	tar \
	tree \
	ttf-liberation \
	unace \
	unrar \
	unzip \
	usb_modeswitch \
	usbutils \
	vim \
	vulkan-icd-loader \
	wavpack \
	wget \
	which \
	wireplumber \
	wireless-regdb \
	wqy-zenhei \
	xdg-desktop-portal \
	xdg-desktop-portal-gnome \
	xdg-desktop-portal-wlr \
	xdg-user-dirs-gtk \
	xf86-video-amdgpu \
	xorg-server \
	xz \
	zip \
"

# Each entry is the clone url (https://aur.archlinux.org/{AUR_PACKAGE}.git)
# Which is often the same as the package name but it can be different.
# Check on the AUR webpage if you are unsure
export AUR_PACKAGES="\
	asusctl \
	ayaneo-platform-dkms-git \
	ayn-platform-dkms-git \
	bcm20702a1-firmware \
	boxtron \
	chimera \
	chimeraos-device-quirks-git \
	downgrade \
	evdev-keepalive \
	frzr \
	gamescope-session-steam-git \
	gamescope-session-steam-plus-git \
	gpd-fan-driver-dkms-git \
	hhfc-git \
	hid-msi-claw-dkms-git \
	legendary \
	libretro-dosbox-pure-git \
	libretro-lrps2-git \
	libretro-opera-git \
	libretro-prosystem-git \
	libretro-stella2014-git \
	libretro-virtualjaguar-git \
	linux-firmware-valve \
	kanit-font \
	nintendo-udev \
	opengamepadui-bin \
	opengamepadui-session-git \
	pikaur \
	powerstation-bin \
	python-gbopyrator \
	python-vdf \
	rtl8812au-dkms-git \
	rtl8814au-dkms-git \
	rtl8821au-dkms-git \
	ryzenadj-git \
	steam_notif_daemon \
	steam-powerbuttond-git \
	steam-removable-media-git \
	wyvern \
	xpadneo-dkms-git \
	zenergy-dkms-git \
"

export SERVICES="\
	NetworkManager \
	avahi-daemon \
	bluetooth \
	bluetooth-workaround \
	fstrim.timer \
	home-swapfile.swap \
	inputplumber \
	inputplumber-suspend \
	lightdm \
	nvidia-powerd \
	pcscd.socket \
	powerstation \
	steam-powerbuttond \
	sshd \
	systemd-timesyncd \
	swapfile \
"

export USER_SERVICES="\
	chimera.service \
	pipewire \
	steam-patch.service \
	chimera-cart-monitor.service \
	chimera-nfc-monitor.service \
"

export FILES_TO_DELETE="\
	/boot/initramfs-linux-fallback.img \
	/usr/share/SFML \
	/usr/share/doc \
	/usr/share/gtk-doc \
	/usr/share/help \
	/usr/share/man \
"

postinstallhook() {
	# Add sudo permissions
	sed -i '/%wheel ALL=(ALL:ALL) ALL/s/^# //g' /etc/sudoers
	echo "${USERNAME} ALL=(ALL) NOPASSWD: /usr/bin/dmidecode -t 11
	" >/etc/sudoers.d/steam
	echo "${USERNAME} ALL=(ALL) NOPASSWD: /usr/bin/chimera-session-use-gamescope
	${USERNAME} ALL=(ALL) NOPASSWD: /usr/bin/chimera-session-use-lightdm
	${USERNAME} ALL=(ALL) NOPASSWD: /usr/share/chimera/bin/power-tool
	" >/etc/sudoers.d/chimera

	# download and add racing wheel udev rules
	pushd /usr/lib/udev/rules.d
	curl -L -O https://raw.githubusercontent.com/berarma/oversteer/master/data/udev/99-fanatec-wheel-perms.rules
	curl -L -O https://raw.githubusercontent.com/berarma/oversteer/master/data/udev/99-logitech-wheel-perms.rules
	curl -L -O https://raw.githubusercontent.com/berarma/oversteer/master/data/udev/99-thrustmaster-wheel-perms.rules
	popd

	# Remove build tools for slimmer image
	rm /usr/share/libalpm/hooks/70-dkms-install.hook
	rm /usr/share/libalpm/hooks/70-dkms-upgrade.hook
	rm /usr/share/libalpm/hooks/71-dkms-remove.hook
	pacman --noconfirm -Rnsdd make gcc dkms ${KERNEL_PACKAGE}-headers

	# remove dolphin shortcut to not clutter up the desktop
	rm /usr/share/applications/dolphin-emu.desktop

	# clean up desktop shortcuts
	sed -i -e 's/Name=Steam (Runtime)/Name=Steam/' /usr/share/applications/steam.desktop
	find /usr/share/applications/* |
		grep -v org.chimeraos.Gamescope.desktop |
		grep -v org.chimeraos.app.desktop |
		grep -v org.gnome.Console.desktop |
		grep -v org.gnome.DiskUtility.desktop |
		grep -v org.gnome.FileRoller.desktop |
		grep -v org.gnome.Nautilus.desktop |
		grep -v org.gnome.Settings.desktop |
		grep -v org.gnome.Software.desktop |
		grep -v org.gnome.TextEditor.desktop |
		grep -v steam.desktop |
		xargs -I {} sh -c "echo NoDisplay=true >> {}"

	# force -steamdeck option in desktop mode to prevent constant steam updates
	sed -i 's,Exec=/usr/bin/steam-runtime,Exec=/usr/bin/steam-runtime -steamdeck,' /usr/share/applications/steam.desktop

	# workaround for dosbox compatibility tool being run unnecessarily by Steam at startup
	sed -i 's,/run-dosbox,/run-dosbox-wrapper,' /usr/share/boxtron/toolmanifest.vdf

	# Disable SPDIF/IEC958 audio output to make it more likely the correct HDMI output will be selected by default
	sed -e '/\[Mapping iec958/,+5 s/^/#/' -i '/usr/share/alsa-card-profile/mixer/profile-sets/default.conf'

	# Replace wpctl with a wrapper to work around Steam resetting audio
	mv /usr/bin/wpctl /usr/libexec/wpctl
	mv /usr/bin/wpctl-wrapper /usr/bin/wpctl
}
